package com.classichu.jetpacksimple.ui.zhihu;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;

import com.classichu.jetpacksimple.R;
import com.classichu.jetpacksimple.adapter.ZhihuListRVAdapter;
import com.classichu.jetpacksimple.data.local.entity.ZhihuStory;
import com.classichu.jetpacksimple.listener.OnEndlessRVOnScrollListener;
import com.classichu.jetpacksimple.listener.OnItemClickListener;
import com.classichu.jetpacksimple.ui.main.MainViewModel;

import java.util.List;

public class ZhihuFragment extends Fragment {

    private ZhihuViewModel mViewModel;

    public static ZhihuFragment newInstance() {
        return new ZhihuFragment();
    }

    private ZhihuListRVAdapter mZhihuListRVAdapter;
    SwipeRefreshLayout mSwipeRefreshLayout;
    ProgressBar mLoadMoreProgressBar;

    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        //        return inflater.inflate(R.layout.main_fragment, container, false);
        View view = inflater.inflate(R.layout.zhihu_fragment, container, false);


        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        mZhihuListRVAdapter = new ZhihuListRVAdapter();
        mZhihuListRVAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                super.onItemClick(view, position);
            }
        });
        RecyclerView recyclerView = view.findViewById(R.id.rv_zhihu_list);
        recyclerView.setAdapter(mZhihuListRVAdapter);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addOnScrollListener(new OnEndlessRVOnScrollListener() {
            @Override
            public void onLoadMore(int lastItemPosition) {
                String flag = "flag_" + lastItemPosition;
                mViewModel.loadMoreZhihuStoryList(flag);
            }

        });
        //
        mSwipeRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.srl_zhihu);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //
                mViewModel.refreshZhihuStoryList();
            }
        });


        mSwipeRefreshLayout.setColorSchemeResources(
                android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);
        //
        mLoadMoreProgressBar = (ProgressBar) view.findViewById(R.id.bar_load_more_zhihu);
        mLoadMoreProgressBar.setVisibility(View.INVISIBLE);

        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mViewModel = ViewModelProviders.of(this).get(ZhihuViewModel.class);
        // TODO: Use the ViewModel
        //数据入口
        mViewModel.getZhihuStoryListLiveData().observe(this, new Observer<List<ZhihuStory>>() {
            @Override
            public void onChanged(@Nullable List<ZhihuStory> zhihuStoryList) {
                mZhihuListRVAdapter.addDataListAtEnd(zhihuStoryList);
                mSwipeRefreshLayout.setRefreshing(false);
            }
        });
        mViewModel.IsLoading().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(@Nullable Boolean aBoolean) {
                //
                mLoadMoreProgressBar.setVisibility(aBoolean ? View.VISIBLE : View.INVISIBLE);
            }
        });
        mViewModel.refreshZhihuStoryList();
        mSwipeRefreshLayout.setRefreshing(true);
//        getLifecycle().addObserver();
    }
}
